home *** CD-ROM | disk | FTP | other *** search
/ Stone Design / Stone Design.iso / Stone_Friends / 3D_Rendering / Musgrave_Shaders / Source / terran.sl < prev    next >
Encoding:
Text File  |  1994-12-08  |  7.0 KB  |  225 lines

  1. /*
  2.  * terran.sl - surface for an Earth-like planet.
  3.  *
  4.  *
  5.  * DESCRIPTION:
  6.  *      When put on a sphere, sets the color to look like relatively
  7.  *   Earth-like.  The shader works by using a variety of fractal 
  8.  *   turbulence and mottling techniques.
  9.  *      Note that there is a companion displacement shader "terranbump"
  10.  *   which is necessary to get the best effect.  If you do this, it is
  11.  *   important that the parameters common to "terran" and "terranbump"
  12.  *   both be set to the same values.  Otherwise you get bumpy mountains
  13.  *   in the middle of the ocean.
  14.  *
  15.  *
  16.  * PARAMETERS:
  17.  *    Ka, Kd - the usual meaning
  18.  *    spectral_exp, lacunarity, octaves - control the fractal characteristics
  19.  *                of the bump pattern.
  20.  *    bump_scale - scaling of the mountains
  21.  *    multifractal - zero uses fBm noise, nonzero uses multifractal
  22.  *    dist_scale - scaling for multifractal distortion
  23.  *    offset - elevation offset
  24.  *    sea_level - obvious
  25.  *    mtn_scale - scaling factor for mountains
  26.  *    lat_scale, nonlinear, purt_scale, map_exp - control scaling of 
  27.  *               terrain type by latitude
  28.  *    ice_caps - latitude at which ice caps tend to form on the oceans
  29.  *    depth_scale, depth_max - scaling factor and max depth of oceans
  30.  *    mottle_limit, mottle_scale, moddle_dim, mottle_mag - control the
  31.  *               mottling that adds detail to lower latitude regions.
  32.  *
  33.  *
  34.  * HINTS:
  35.  *       The default values for the shader assume that the planet is
  36.  *    represented by a unit sphere.  The texture space and/or parameters
  37.  *    to this shader will need to be altered if the size of your planet
  38.  *    is radically different.
  39.  *       For best results, use with the "terranbump" displacement shader,
  40.  *    and add a cloud layer using either "planetclouds" or "venusclouds".
  41.  *
  42.  *
  43.  * AUTHOR: Ken Musgrave.
  44.  *    Conversion to Shading Language and minor modifications by Larry Gritz.
  45.  *
  46.  *
  47.  * REFERENCES:
  48.  *
  49.  *
  50.  * HISTORY:
  51.  *    ???? - original texture developed by F. Ken Musgrave.
  52.  *    Feb 1994 - Conversion to Shading Language by L. Gritz
  53.  *
  54.  * last modified 1 March 1994 by lg
  55.  */
  56.  
  57.  
  58. #ifdef BMRT
  59. #define snoise(x) (2*(noise(x)-0.5))
  60. #else
  61. /* This is because PRMAN's noise has less range than BMRT's */
  62. #define snoise(x) (2.5*(noise(x)-0.5))
  63. #endif
  64.  
  65. #define DNoise(x) ((2*(point noise(x))) - point(1,1,1))
  66. #define VLNoise(Pt,scale) (snoise(DNoise(Pt)+(scale*Pt)))
  67. #define N_OFFSET 0.7
  68. #define VERY_SMALL 0.0001
  69.  
  70.  
  71. surface
  72. terran (float Ka = .5, Kd = .7;
  73.     float spectral_exp = 0.5;
  74.     float lacunarity = 2, octaves = 7;
  75.     float bump_scale = 0.07;
  76.     float multifractal = 0;
  77.     float dist_scale = .2;
  78.     float offset = 0;
  79.     float sea_level = 0;
  80.     float mtn_scale = 1;
  81.     float lat_scale = 0.95;
  82.     float nonlinear = 0;
  83.     float purt_scale = .9;
  84.     float map_exp = 0;
  85.     float ice_caps = 0.9;
  86.     float depth_scale = 1;
  87.     float depth_max = .5;
  88.     float mottle_limit = 0.75;
  89.     float mottle_scale = 20;
  90.     float mottle_dim = .25;
  91.     float mottle_mag = .02;)
  92. {
  93.   point PP;
  94.   point PtN;
  95.   float chaos, latitude, purt;
  96.   color Ct;
  97.   point Ptexture, tp;
  98.   float l, o, a, i, weight;      /* Loop variables for fBm calc */
  99.   float bumpy;
  100.  
  101.   /* Do all shading in shader space */
  102.   Ptexture = transform ("shader", P);
  103.   PtN = normalize (Ptexture);      /* Version of Ptexture with radius 1 */
  104.  
  105.   /**********************************************************************
  106.    * First, figure out where we are in relation to the oceans/mountains.
  107.    * Note: this section of code must be identical to "terranbump" if you
  108.    *       expect these two shaders to work well together.
  109.    **********************************************************************/
  110.  
  111.   if (multifractal == 0) {    /* use a "standard" fBm bump function */
  112.       o = 1;  l = 1;  bumpy = 0;
  113.       for (i = 0;  i < octaves;  i += 1) {
  114.       bumpy += o * snoise (l * Ptexture);
  115.       l *= lacunarity;
  116.       o *= spectral_exp;
  117.         }
  118.     }
  119.   else {            /* use a "multifractal" fBm bump function */
  120.       /* get "distortion" vector, as used with clouds */
  121.       Ptexture += dist_scale * DNoise (Ptexture);
  122.       /* compute bump vector using MfBm with displaced point */
  123.       o = spectral_exp;  tp = Ptexture;
  124.       weight = abs (VLNoise (tp, 1.5));
  125.       bumpy = weight * snoise (tp);
  126.       for (i = 1;  i < octaves  &&  weight >= VERY_SMALL;  i += 1) {
  127.       tp *= lacunarity;
  128.       /* get subsequent values, weighted by previous value */
  129.       weight *= o * (N_OFFSET + snoise(tp));
  130.       weight = clamp (abs(weight), 0, 1);
  131.       bumpy += snoise(tp) * min (weight, spectral_exp);
  132.       o *= spectral_exp;
  133.     }
  134.     }
  135.  
  136.   /* get the "height" of the bump, displacing by offset */
  137.   chaos = bumpy + offset;
  138.   /* set bump for land masses (i.e., areas above "sea level") */
  139.   if (chaos > sea_level) {
  140.       chaos *= mtn_scale;
  141. /*      sea_level *= mtn_scale; */
  142.     }
  143.  
  144.  
  145.   /************************************************************************
  146.    * Step 2: Assign a climite type, roughly by latitude.
  147.    ************************************************************************/
  148.  
  149.   /* make climate symmetric about equator -- use the "v" parameter */
  150.   latitude = abs (zcomp (PtN));
  151.  
  152.   /* fractally purturb color map offset using "chaos" */
  153.   /*  "nonlinear" scales purturbation-by-z */
  154.   /*  "purt_scale" scales overall purturbation */
  155.   latitude += chaos*(nonlinear*(1-latitude) + purt_scale);
  156.   if (map_exp > 0)
  157.        latitude = lat_scale * pow(latitude,map_exp);
  158.   else latitude *= lat_scale;
  159.  
  160.  
  161.   if (chaos > sea_level) {
  162.       /* Choose color of land based on the following spline.
  163.        * Ken originally had a huge table.  I was too lazy to type it in,
  164.        * so I used a scanned photo of the real Earth to select some
  165.        * suitable colors.  -- lg
  166.        */
  167. /*
  168.       Ct = spline (latitude,
  169.            color (.529, .412, .2745),
  170.            color (.529, .412, .2745),
  171.            color (.529, .412, .2745),
  172.            color (.255, .341,  0),
  173.            color (.256, .341, .141),
  174.            color (.235, .392, .235),
  175.            color (.490, .494, .1176),
  176.            color (.655, .529, .392),
  177.            color (.769, .616, .314),
  178.            color (.976, .820, .471),
  179.            color (1,1,1),
  180.            color (1,1,1));
  181. */
  182.       Ct = spline (latitude,
  183.            color (.5, .39, .2),
  184.            color (.5, .39, .2),
  185.            color (.5, .39, .2),
  186.            color (.2, .3,  0),
  187.            color (.085, .2, .04),
  188.            color (.065, .22, .04),
  189.            color (.5, .42, .28),
  190.            color (.6, .5, .23),
  191. /*           color (.976, .820, .471), */
  192.            color (1,1,1),
  193.            color (1,1,1));
  194.  
  195.      /* mottle the color some */
  196.      if (latitude < mottle_limit) {
  197.          PP = mottle_scale * Ptexture;
  198.      l = 1;  o = 1;
  199.      for (i = 0;  i < 6;  i += 1) {
  200.          purt += o * snoise (l * PP);
  201.          l *= 2;
  202.          o *= mottle_dim;
  203.        }
  204.      Ct += (mottle_mag * purt) * (color (0.5,0.175,0.5));
  205.        }
  206.     }
  207.   else { 
  208.       /* Oceans */
  209.       Ct = color(.1,.2,.5);
  210.       if (ice_caps > 0  &&  latitude > ice_caps)
  211.       Ct = color(1,1,1);  /* Ice color */
  212.       else {
  213.       /* Adjust color of water to darken deeper seas */
  214.           chaos -= sea_level;
  215.       chaos *= depth_scale;
  216.       chaos = max (chaos, -depth_max);
  217.       Ct *= (1+chaos);
  218.         }
  219.     }
  220.  
  221.   /* Shade using matte model */
  222.   Oi = 1;
  223.   Ci = Os * Ct * (Ka * ambient() + Kd * diffuse(faceforward(normalize(N),I)));
  224. }
  225.